#ifndef __Process__Pool__Hpp__
#define __Process__Pool__Hpp__

#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <vector>
#include "Task.hpp"
#include <sys/wait.h>
class Channel
{
public:
    Channel(int wfd, pid_t subpid)
        : _wfd(wfd), _subpid(subpid)
    {
        _name = "Channel:" + std::to_string(wfd) + " mypid:" + std::to_string(subpid);
    }
    ~Channel()
    {
    }

    void Send(int code)
    {
        ssize_t n = write(_wfd, &code, sizeof(code));
        (void)n;
    }

    void Stop()
    {
        close(_wfd);
    }

    void Wait()
    {
        pid_t pid = waitpid(_subpid, nullptr, 0);
        (void)pid;
    }

    std::string Name()
    {
        return _name;
    }

    int GetWfd()
    {
        return _wfd;
    }
private:
    int _wfd;
    pid_t _subpid;
    std::string _name;
};

class ChannelManager
{
public:
    ChannelManager() : _next(0)
    {
    }
    ~ChannelManager()
    {
    }

    void Insert(int wfd, pid_t subpid)
    {
        _cv.emplace_back(wfd, subpid);
    }

    Channel &Select()
    {
        auto &c = _cv[_next++];
        _next %= _cv.size();
        return c;
    }

    void PrintfChannel()
    {
        for (Channel &c : _cv)
            std::cout << "我的name:" << c.Name() << std::endl;
    }

    void StopSubProcess()
    {
        for (Channel &Channel : _cv)
        {
            Channel.Stop();
            std::cout << "结束子进程:" << Channel.Name() << std::endl;
        }
    }

    void WaitSubProcess()
    {
        for (Channel &Channel : _cv)
        {
            Channel.Wait();
            std::cout << "等待子进程:" << Channel.Name() << std::endl;
        }
    }

    void CloseOther()
    {
        for(int i=0;i<_cv.size();i++)
        {
            close(_cv[i].GetWfd());
            std::cout<<"关闭通道:"<<_cv[i].Name()<<std::endl;
        }
    }
private:
    std::vector<Channel> _cv;
    int _next;
};

class ProcessPool
{
public:
    ProcessPool(int num) : _process_num(num)
    {
        _tm.Register(Download);
        _tm.Register(Upload);
        _tm.Register(Pintfload);
    }

    ~ProcessPool()
    {
    }

    void Work(int rfd)
    {
        while (true)
        {
            int code = 0;
            ssize_t n = read(rfd, &code, sizeof(code));
            if (n > 0)
            {
                if (n != sizeof(code))
                {
                    continue;
                }
                else
                {
                    std::cout << "我是子进程pid[" << getpid() << "]收到的任务码:" << code << std::endl;
                    _tm.Excute(code);
                }
            }
            else if (n == 0)
            {
                std::cout << "子进程退出pid:" << getpid() << std::endl;
                break;
            }
            else
            {
                std::cout << "read error" << std::endl;
                break;
            }
        }
    }

    bool Start()
    {
        for (int i = 0; i < _process_num; i++)
        {
            int pipefd[2] = {0};
            int n = pipe(pipefd);
            if (n < 0)
            {
                std::cout << "pipe error" << std::endl;
                return false;
            }

            pid_t subpid = fork();
            if (subpid < 0)
                return false;
            else if (subpid == 0)
            {
                // child
                close(pipefd[1]); // 关闭不必要的管道
                _cm.CloseOther();
                Work(pipefd[0]);
                close(pipefd[0]);
                exit(0);
            }
            else
            {
                // father
                sleep(1);
                close(pipefd[0]);
                _cm.Insert(pipefd[1], subpid);
            }
        }
        return true;
    }

    void Run()
    {

        int taskcode = _tm.code();
        auto &c = _cm.Select();
        std::cout << "选择一个子进程:" << c.Name() << std::endl;

        c.Send(taskcode);
        std::cout << "发送一个任务码:" << taskcode << std::endl;
    }

    void Debug()
    {
        _cm.PrintfChannel();
    }

    void Stop()
    {
        // 关闭父进程所有的wfd即可
        _cm.StopSubProcess();
        // 回收所有子进程
        _cm.WaitSubProcess();
    }

private:
    ChannelManager _cm;
    int _process_num;
    TaskManager _tm;
};

#endif
